import type { GetFile, GetFileRes } from '#common/types/file';

const selectFolder = async (): Promise<string | undefined> =>
  await window.clientAppSdk.ipc.ipcRenderer.invoke('rc:select-folder');

const getFile = async (param: GetFile): Promise<string | ArrayBuffer | undefined> => {
  if (!param.directory || !param.fileName) return;

  const file: GetFileRes = await window.clientAppSdk.ipc.ipcRenderer.invoke(
    'rc:read-file-content',
    {
      directory: param.directory,
      fileName: param.fileName,
      readType: param.readType
    } satisfies GetFile
  );
  if (file) return file.data;
  return;
};

const writeFile = async (directory: string, fileName: string, content: string) => {
  if (!directory || !fileName) {
    return;
  }
  const meta = await window.clientAppSdk.ipc.ipcRenderer.invoke('rc:write-fileReplace-sync', {
    directory,
    fileName,
    content
  });
  return meta;
};

const getVideoMeta = async (filepath: string) => {
  if (!filepath) {
    return;
  }
  const meta = await window.clientAppSdk.ipc.ipcRenderer.invoke('rc:read-video-mete', filepath);
  return meta;
};

export { selectFolder, getFile, writeFile, getVideoMeta };
